﻿DECLARE @Var_CallNo          VARCHAR(100) SET @Var_CallNo          = @CallNo_In   
DECLARE @Var_CallName        VARCHAR(100) SET @Var_CallName        = @CallName_In 
DECLARE @Var_PatientId       VARCHAR(100) SET @Var_PatientId       = @PatientId_In
DECLARE @Var_DeptCode        VARCHAR(100) SET @Var_DeptCode        = @DeptCode_In     
DECLARE @Var_DoctCode        VARCHAR(100) SET @Var_DoctCode        = @DoctCode_In
DECLARE @Var_ReservationDate DATE         SET @Var_ReservationDate = @ReservationDate_In 

DECLARE @HIS_RegisterId     INT
DECLARE @HIS_PatientName    VARCHAR(100)
DECLARE @HIS_DeptName       VARCHAR(100)
DECLARE @HIS_Address        VARCHAR(100) 
DECLARE @HIS_DoctName       VARCHAR(100)
DECLARE @HIS_CurrentNumber  VARCHAR(100)
DECLARE @HIS_QueueNumber    VARCHAR(100)
DECLARE @HIS_OriginAmount   MONEY
DECLARE @HIS_TotalAmount    MONEY
DECLARE @HIS_TimeInterval   VARCHAR(100)
DECLARE @HIS_CheckGroupCode VARCHAR(100)
DECLARE @HIS_CheckGroupName VARCHAR(100)
DECLARE @HIS_CurrentDate    DATETIME
DECLARE @HIS_PatientSex     VARCHAR(20)
DECLARE @HIS_DoctTitleCode  VARCHAR(20)
DECLARE @HIS_DoctTitleName  VARCHAR(100)
DECLARE @HIS_OperCode VARCHAR(100)

SET @HIS_CurrentDate = GETDATE()


IF @Var_ReservationDate <= @HIS_CurrentDate OR @Var_ReservationDate > @HIS_CurrentDate + 7
BEGIN
    SELECT  -1 AS RESULT, '预约日期超出范围！' AS RESULTTEXT;
    RETURN;
END;

SELECT @HIS_OperCode=CODE FROM YSCODE(NOLOCK) WHERE NAME=@Var_CallName
IF @HIS_OperCode IS NULL
BEGIN
    SELECT -1 AS RESULT,'未找到操作员代码，请联系开发商！' AS RESULTTEXT
    RETURN
END

SELECT @HIS_PatientName = PatientName,
       @HIS_PatientSex = CASE WHEN Sex = '男' THEN 1 WHEN Sex = '女' THEN 2 ELSE 4 END 
  FROM PatientBase (NOLOCK) 
 WHERE PATIENTID=@Var_PatientId

IF @HIS_PatientName IS NULL
BEGIN
    SELECT -1 AS RESULT,'未找到PATIENTID对应的患者，或未绑定卡！' AS RESULTTEXT
    RETURN
END;

IF EXISTS(SELECT Id FROM dbo.AP_PreRegist_DongRuan(NOLOCK) WHERE PatientId=@Var_PatientId AND RegistDate=@Var_ReservationDate AND ISReservation = 1 )
BEGIN
  SELECT -1 AS RESULT,'同一天内只能预约一次！' AS RESULTTEXT
  RETURN
END

/*SET @HIS_CurrentDate = GETDATE();*/

SELECT @HIS_DeptName       = A.KSName     
      ,@HIS_Address        = RTRIM(C.KSAddress)
      ,@HIS_DoctName       = A.YSName
      ,@HIS_CurrentNumber  = ''
      ,@HIS_QueueNumber    = ''
      ,@HIS_OriginAmount   = A.CheckMoney1 + A.CheckMoney2
      ,@HIS_TotalAmount    = A.CheckMoney1 + A.CheckMoney2
      ,@HIS_TimeInterval   = A.TimeInterval
      ,@HIS_CheckGroupCode = A.CheckGroupCode
      ,@HIS_CheckGroupName = A.CheckGroupName
      ,@HIS_DoctTitleCode  = B.YHFlag
      ,@HIS_DoctTitleName  = CASE B.YHFLAG WHEN 2 THEN '普通医师（初职）'
                                           WHEN 3 THEN '主任医师（高职）'
                                           WHEN 5 THEN '主治医师（中职）'
                                           WHEN 6 THEN '副主任医师（副高）'
                             END
  FROM MZSchedule A(NOLOCK) LEFT JOIN YSCode B(NOLOCK) ON A.YSCode = B.Code 
  LEFT JOIN dbo.KSCODE C(NOLOCK) ON A.KSCode = C.CODE
 WHERE A.KSCode = @Var_DeptCode AND 
       A.YSCode = @Var_DoctCode AND 
       A.Weekday = DATEPART(WEEKDAY,@Var_ReservationDate) AND
       (STATUS IS NULL OR STATUS=2) 

declare @HIS_Totnum int
declare @HIS_Totnum2 int
declare @HIS_OtherAlreadyCount int

SELECT @HIS_Totnum=ISNULL(totnum,0),@HIS_Totnum2=ISNULL(TOTNUM2,0) FROM MZSCHEDULE (NOLOCK) WHERE KSCODE=@Var_DeptCode AND YSCODE=@Var_DoctCode AND TIMEINTERVAL=@HIS_TimeInterval 
AND CHECKGROUPCODE=@HIS_CheckGroupCode AND 
(STATUS IS NULL OR STATUS=2) AND TIMEINTERVAL IS NOT NULL AND 
WEEKDAY=DATEPART(WEEKDAY,@Var_ReservationDate)

IF @HIS_Totnum IS NULL
BEGIN
    SELECT -1 AS RESULT,'挂号目标不正确，请核对，有可能是医院调整了排班信息！' AS RESULTTEXT
    RETURN
END

SELECT @HIS_OtherAlreadyCount=COUNT(*) FROM AP_RESERVATIONSHEET(NOLOCK) 
WHERE DEPTCODE=@Var_DeptCode AND DOCTCODE=@Var_DoctCode AND DELDATE IS NULL AND RESERVATIONDATE=@Var_ReservationDate

IF @HIS_TotNum > 0
BEGIN
  IF (@HIS_TotNum - @HIS_OtherAlreadyCount <= 0) OR (@HIS_OtherAlreadyCount >= @HIS_Totnum2)
  BEGIN
    SELECT -1 AS RESULT,'该医师号源已预约完了，请择期再试！' AS RESULTTEXT
    RETURN
  END
END

IF @HIS_DeptName IS NULL
BEGIN
    SELECT -1 AS RESULT,'没有找到相应的号源，请核对后重试！' AS RESULTTEXT
    RETURN
END

INSERT INTO [AP_PreRegist_DongRuan]
           ([PatientId]
           ,[PatientName]
           ,[KSCode]
           ,[KSName]
           ,[YSCode]
           ,[YSName]
           ,[TitleCode]
           ,[TitleName]
           ,[Address]
           ,[CheckGroupCode]
           ,[CheckGroupName]
           ,[RegistMoney]
           ,[PayMoney]
           ,[TimeInterval]
           ,[RegistDate]
           ,[PreOperDate]
           ,[IsFinish]
           ,[Sex]
           ,[clinicRange]
           ,[duty]
           ,[IsReservation])
     VALUES
           (@Var_PatientId
           ,@HIS_PatientName
           ,@Var_DeptCode
           ,@HIS_DeptName
           ,@Var_DoctCode
           ,@HIS_DoctName
           ,@HIS_DoctTitleCode
           ,@HIS_DoctTitleName
           ,@HIS_Address
           ,@HIS_CheckGroupCode
           ,@HIS_CheckGroupName
           ,@HIS_OriginAmount
           ,@HIS_TotalAmount
           ,@HIS_TimeInterval
           ,@Var_ReservationDate
           ,DATEADD(MINUTE,15,@HIS_CurrentDate)
           ,0
           ,@HIS_PatientSex
           ,@HIS_TimeInterval
           ,@HIS_DoctTitleName
           ,1)

SELECT 1 AS RESULT
      ,'成功' AS RESULTTEXT
      ,Id AS RegisterId   
      ,@HIS_DeptName      AS DeptName     
      ,@HIS_Address       AS Address
      ,@HIS_DoctName      AS DoctName     
      ,@HIS_CurrentNumber AS CurrentNumber
      ,@HIS_QueueNumber   AS QueueNumber 
      ,@HIS_TimeInterval  AS TimeInterval
      ,@HIS_OriginAmount  AS OriginAmount 
      ,@HIS_TotalAmount   AS TotalAmount  
  FROM [AP_PreRegist_DongRuan]
 WHERE @@ROWCOUNT > 0 AND Id = scope_identity()